package com.magicsoft.share.adapter.article;

import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseItemDraggableAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.magicsoft.share.R;
import com.magicsoft.share.bean.article.ArticleContent;

import java.util.List;

/**
 *
 * @author lyf
 * @date 2017/11/10/010
 */

public class EditArticleAdapter extends BaseItemDraggableAdapter<ArticleContent, BaseViewHolder> {


    private final TranslateAnimation mShowAction1;
    private final TranslateAnimation mShowAction2;
    private final TranslateAnimation mHiddenAction;
    private final RequestOptions options;
    private boolean isShow;
    private ImageView ivAdd;
    private ImageView ivAddImg;
    private ImageView ivAddText;
    private ImageView imageView;
    private int clickPosition = 0;

    public EditArticleAdapter(int layoutResId, List<ArticleContent> data) {
        super(layoutResId, data);
        mShowAction1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -0.5f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        mShowAction1.setDuration(500);
        mShowAction2 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1.5f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        mShowAction2.setDuration(500);

        mHiddenAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                0.0f);
        mHiddenAction.setDuration(500);
        options = new RequestOptions()

                .placeholder(R.drawable.text)
                .error(R.drawable.text);
    }


    @Override
    protected void convert(BaseViewHolder helper, ArticleContent item) {

        ivAdd = helper.getView(R.id.iv_add);
        ivAddImg = helper.getView(R.id.iv_add_img);
        imageView = helper.getView(R.id.imageView);
        ivAddText = helper.getView(R.id.iv_add_text);

        helper
                .addOnClickListener(R.id.iv_add)
                .addOnClickListener(R.id.iv_close)
                .addOnClickListener(R.id.iv_add_img)
                .addOnClickListener(R.id.imageView)
                .addOnClickListener(R.id.iv_add_text)
                .addOnClickListener(R.id.tv_content);
        if (!item.getImg().equals("") && item.getImg()!=null) {
                Glide.with(mContext).load(item.getImg()).apply(options).into(imageView);
//            Log.e("dataa",item.getImg());
        }


        helper.setText(R.id.tv_content, item.getContent());


    }

    public void addShowOrHide(int position) {
        clickPosition = position;
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        if (position == clickPosition) {
            if (!isShow) {
                ivAdd.animate().rotation(45);
                ivAddImg.startAnimation(mShowAction2);
                ivAddImg.setVisibility(View.VISIBLE);
                ivAddText.startAnimation(mShowAction1);
                ivAddText.setVisibility(View.VISIBLE);
                isShow = true;
            } else {
                ivAdd.animate().rotation(180);
                ivAddImg.startAnimation(mHiddenAction);
                ivAddImg.setVisibility(View.INVISIBLE);
                ivAddText.startAnimation(mHiddenAction);
                ivAddText.setVisibility(View.INVISIBLE);
                isShow = false;
            }
        }
    }

    public void updatae(int i) {
      notifyItemChanged(i);
    }
    public void isShow(boolean isShow){
        this.isShow = isShow;
    }

    public void setImg(int i, ArticleContent articleContent) {
        imageView.setImageResource(R.drawable.t);
    }
}
